/** 比较器函数类型接口 */
export type ICompareFunc<T> = (a: T, b: T) => number;
export type IDiffFunc<T> = (a: T, b: T) => boolean;

/**
 * #### 比较结果枚举
 */
export enum Compare {
  /** 小于 */
  LESS_THAN = -1,
  /** 等于 */
  EQUALS = 0,
  /** 大于 */
  GREATER_THAN = 1,
}

/**
 * #### 默认的比较函数
 * @param a 元素a
 * @param b 元素b
 * @returns 0|1|-1
 */
export function defaultCompare<T>(a: T, b: T): number {
  return a === b
    ? Compare.EQUALS
    : a < b
    ? Compare.LESS_THAN
    : Compare.GREATER_THAN;
}

/**
 * #### 反转比较函数
 * @param compareFn 比较函数
 * @returns
 */
export function reverseCompare<T>(compareFn: ICompareFunc<T>): ICompareFunc<T> {
  return (a: T, b: T) => compareFn(b, a);
}

export function defaultDiff<T>(a: T, b: T) {
  return Number(a) - Number(b);
}

/**
 * #### 大于等于
 * @param a 元素a
 * @param b 元素b
 * @param compareFn 比较函数
 * @returns
 */
export function biggerEquals<T>(a: T, b: T, compareFn: ICompareFunc<T>) {
  const comp = compareFn(a, b);
  return comp === Compare.GREATER_THAN || comp === Compare.EQUALS;
}

/**
 * 小于等于
 * @param a 元素a
 * @param b 元素b
 * @param compareFn 比较函数
 * @returns
 */
export function lesserEquals<T>(a: T, b: T, compareFn: ICompareFunc<T>) {
  const comp = compareFn(a, b);
  return comp === Compare.LESS_THAN || comp === Compare.EQUALS;
}

/**
 * 判断是否为空
 * @param value 值
 * @returns
 */
export function isEmpty(value: unknown) {
  return value === null || value === undefined;
}

/**
 * #### 默认相等
 * @param a 元素a
 * @param b 元素a
 * @returns
 */
export function defaultEquals<T>(a: T, b: T) {
  return a === b;
}

export function defaultToString(item: any) {
  if (isEmpty(item)) {
    return item + "";
  } else if (typeof item === "string" || item instanceof String) {
    return `${item}`;
  }
  return item.toString();
}
